package com.atguigu.yygh.user.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "就诊人服务管理")
@RestController
@RequestMapping("/api/user/patient/auth")
public class ApiPatientController {
    @Autowired
    private PatientService patientService;

    /**
     * todo: 获取就诊列表接口
     * @param request
     * @return
     */
    @ApiOperation("获取就诊列表接口")
    @GetMapping("findAll")
    public Result findAll(HttpServletRequest request) {
        // 获取请求对象的id
        Long userId = AuthContextHolder.getUserId(request);
        // 调用service方法查询就诊人列表
        List<Patient> patientList = patientService.findAllPatient(userId);
        // 返回数据集合
        return Result.ok(patientList);
    }

    @ApiOperation("就诊人指定查询")
    @GetMapping("findById/{id}")
    public Result getPatientById(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }


    @ApiOperation("添加就诊人信息")
    @PostMapping("savePatient")
    public Result savePatient(HttpServletRequest request, @RequestBody Patient patient) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    @ApiOperation("修改就诊人信息")
    @PutMapping("editPatient")
    public Result editPatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.ok();
    }

    @ApiOperation("移除就诊人信息")
    @DeleteMapping("removePatientById/{id}")
    public Result removePatient(@PathVariable("id") Long id) {
        Patient patient = patientService.getById(id);
        patient.setIsDeleted(1);
        patientService.updateById(patient);
        return Result.ok();
    }

    @ApiOperation("就诊人指定查询订单需求调用")
    @GetMapping("inner/getPatient/{id}")
    public Patient getPatientOfOrderById(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return patient;
    }

}
